package com.ffcs.crmd.tsp.admin.service.impl;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.Cluster;
import com.ffcs.crmd.tsp.admin.service.IClusterService;
import com.ffcs.crmd.tsp.admin.service.ITspClientService;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.PropertiesUtil;

@Service
public class ClusterServiceImpl implements IClusterService {
    
    @Autowired
    private ITspClientService tspClientService;
    
    @Override
    public void saveCluster(Cluster cluster) throws TspException {
        try {
            Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
            properties.setProperty(cluster.getName(), cluster.getServerAddrs());
            
            FileWriter writer = new FileWriter(PropertiesUtil.getInstance().getWebPath("conf/cluster.properties"));
            properties.store(writer, null);
            writer.close();
            tspClientService.add(cluster.getName(), cluster.getServerAddrs());
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void updateCluster(Cluster cluster) throws TspException {
        try {
            Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
            properties.setProperty(cluster.getName(), cluster.getServerAddrs());
            
            FileWriter writer = new FileWriter(PropertiesUtil.getInstance().getWebPath("conf/cluster.properties"));
            properties.store(writer, null);
            writer.close();
            tspClientService.update(cluster.getName(), cluster.getServerAddrs());
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void deleteCluster(Cluster cluster) throws TspException {
        try {
            Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
            properties.remove(cluster.getName());
            FileWriter writer = new FileWriter(PropertiesUtil.getInstance().getWebPath("conf/cluster.properties"));
            properties.store(writer, null);
            writer.close();
            tspClientService.remove(cluster.getName());
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void deleteCluster(List<Cluster> clusterList) throws TspException {
        for (Cluster cluster : clusterList) {
            deleteCluster(cluster);
        }
    }
    
    @Override
    public List<Cluster> query() throws TspException {
        return query(null);
    }
    
    @Override
    public List<Cluster> query(String name) throws TspException {
        try {
            List<Cluster> clusterList = new ArrayList<Cluster>();
            Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
            Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();
            while (it.hasNext()) {
                Entry<Object, Object> entry = it.next();
                String clusterName = (String) entry.getKey();
                String serverAddrs = (String) entry.getValue();
                if (StringUtils.isNoneBlank(clusterName)) {
                    if (StringUtils.isNoneBlank(name) && clusterName.indexOf(name) == -1) {
                        continue;
                    }
                    Cluster cluster = new Cluster();
                    cluster.setName(clusterName);
                    cluster.setServerAddrs(serverAddrs);
                    clusterList.add(cluster);
                }
            }
            return clusterList;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public com.ctg.itrdc.platform.common.entity.PageInfo<Cluster> queryPage(int pageNumber, int pageSize) throws TspException {
        return queryPage(null, pageNumber, pageSize);
    }
    
    @Override
    public com.ctg.itrdc.platform.common.entity.PageInfo<Cluster> queryPage(String name, int pageNumber, int pageSize) throws TspException {
        try {
            List<Cluster> clusterList = query(name);
            int totalCounts = clusterList.size();
            int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
            int end = PageInfo.getPageEndSize(first, pageSize, totalCounts);
            clusterList = clusterList.subList(first, end);
            PageInfo<Cluster> pageInfo = new PageInfo<Cluster>(clusterList);
            pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, clusterList);
            
            com.ctg.itrdc.platform.common.entity.PageInfo<Cluster> rpageInfo = new com.ctg.itrdc.platform.common.entity.PageInfo<Cluster>();
            BeanUtils.applyIf(rpageInfo, pageInfo);
            return rpageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
}
